import json
from datetime import datetime,timedelta

import jwt
from django.core.paginator import Paginator
from django.forms import model_to_dict
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods

from .models import *
from django.http import JsonResponse
@csrf_exempt
@require_http_methods(['POST'])
def login(request):
    data=json.loads(request.body)
    username=data['username']
    password=data['password']

    mi='123456'
    t=datetime.now()+timedelta(minutes=30)
    pay={'username':username,'password':password,'exp':int(t.timestamp())}
    token=jwt.encode(pay,mi)
    return JsonResponse({
        'msg':"登陆成功",
        'token':token
    })

@csrf_exempt
@require_http_methods(['GET'])
def bumenlist(request):
    data=bumen.objects.all()
    list=[]
    for i in data:
        list.append({
            "id":i.id,
            "bmname":i.bmname,
            "sjbmid_id":i.sjbmid_id,
            "cjtime":i.cjtime
        })
    return JsonResponse({
        'msg': "查询成功",
        'data':list
    })

@csrf_exempt
@require_http_methods(['POST'])
def yuangongadd(request):
    data=json.loads(request.body)
    a=yuangong.objects.create(
        ygname=data['ygname'],
        ygbm_id=data['ygbm_id'],
        price=data['price'],
        rztime=data['rztime'],
        ygbh=data['ygbh'],
        zhuangtai=data['zhuangtai']
    )
    return JsonResponse({
        'msg':"添加成功",
        'data':model_to_dict(a)
    })

@csrf_exempt
@require_http_methods(['DELETE'])
def yuangongsha(request,id):
    yuangong.objects.get(id=id).delete()

    return JsonResponse({
        'msg':"删除成功"
    })


@csrf_exempt
@require_http_methods(['PUT'])
def yuangongxiu(request,id):
    data=json.loads(request.body)
    a=yuangong.objects.get(id=id)
    a.ygname=data['ygname']
    a.ygbm_id = data['ygbm_id']
    a.price = data['price']
    a.rztime = data['rztime']
    a.ygbh = data['ygbh']
    a.zhuangtai = data['zhuangtai']
    a.save()
    return JsonResponse({
        'msg':"修改成功",
        'data':model_to_dict(a)
    })


@csrf_exempt
@require_http_methods(['GET'])
def yuangonglist(request):
    data = yuangong.objects.all()
    ygbm_id=request.GET.get('ygbm_id')
    if ygbm_id:
        data=data.filter(ygbm_id=ygbm_id)
    page=request.GET.get('page',1)
    size=request.GET.get('size')or 10
    fenye=Paginator(data,size)
    data=fenye.get_page(page)
    list = []
    for i in data:
        list.append({
            "id":i.id,
            "ygname": i.ygname,
            "ygbm_id": i.ygbm_id,
            "price": i.price,
            "rztime": i.rztime,
            "ygbh": i.ygbh,
            "zhuangtai": i.zhuangtai,
            "bmname":i.ygbm.bmname
        })
    return JsonResponse({
        'msg': "查询成功",
        'data': list
    })


@csrf_exempt
@require_http_methods(['PUT'])
def zhuangtaixiugai(request,id):
    data=json.loads(request.body)
    a = yuangong.objects.get(id=id)
    kao=kaoqin.objects.get(yuangongid_id=id)
    if kao.kqleixing=="1":
        a.zhuangtai = data['zhuangtai']
        a.save()
        return JsonResponse({
            'msg': "修改成功",
            'data': model_to_dict(a),
            'leixing':kao.kqleixing
        })


    return JsonResponse({
        'msg':"离职前请先处理考勤"
    })

@csrf_exempt
@require_http_methods(['POST'])
def kaoqinadd(request):
    data=json.loads(request.body)
    yuangongid_id=data['yuangongid_id']
    kqtime=data['kqtime']
    kqleixing=data['kqleixing']
    beizhu=data['beizhu']
    a=kaoqin.objects.create(
        yuangongid_id=yuangongid_id,
        kqtime=kqtime,
        kqleixing=kqleixing,
        beizhu=beizhu
    )
    return JsonResponse({
        'msg': "添加成功",
        'data': model_to_dict(a)
    })



@require_http_methods(['GET'])
def kaoqinlist(request):
    data=kaoqin.objects.all()
    page = request.GET.get('page', 1)
    size = request.GET.get('size') or 10
    fenye = Paginator(data, size)
    data = fenye.get_page(page)
    list=[]
    for i in data:
        list.append({
            'yuangongid_id':i.yuangongid_id,
            'kqtime':i.kqtime,
            'kqleixing':i.kqleixing,
            'beizhu':i.beizhu
        })
    return JsonResponse({
        'msg': "查询成功",
        'data': list
    })











# Create your views here.
